package chenji.gunapi.gun;

import org.bukkit.entity.Player;

import chenji.gunapi.gun.SoundHandler.SoundType;

public class ReloadHandler implements Cloneable {
	private int initializeTime;
	private int shootspeed;
	private int reloadTime;
	private int tick;
	private boolean canShoot;
	private boolean isReloading;
	private boolean isInitializeing;
	private boolean lastshoot;
	private boolean alreadinit;
	private int nowAmount;
	private int defaultamount;
	private SoundHandler sound;
	private Player p;

	/**
	 * 构造，单位均为tick
	 * 
	 * @param i
	 *            枪械持有后初始化的时间，
	 * @param j
	 *            枪械的发射间隔。
	 * @param x
	 *            枪械的换弹时间。
	 * @param hd
	 */
	public ReloadHandler(int i, int j, int x, int defaultamount) {
		this.initializeTime = i;
		this.shootspeed = j;
		this.reloadTime = x;
		this.isInitializeing = true;
		this.isReloading = false;
		this.defaultamount = defaultamount;
		this.tick = 0;

	}

	private ReloadHandler(int i, int j, int x, int defaultamount, SoundHandler hd, Player p, int amount) {
		this(i, j, x, defaultamount);
		this.sound = hd;
		this.p = p;
		this.sound.playSound(SoundType.RELOAD, p.getLocation());
		this.nowAmount = amount;
	}

	public void tick() {
		this.tick++;
		if (this.nowAmount <= 0) {
			this.canShoot = false;
			return;
		}
		if (this.isReloading) {
			this.canShoot = false;
		}
		if (this.isInitializeing) {
			this.canShoot = false;
		}
		if (this.isReloading) {
			if (this.reloadTime == this.tick) {
				this.isReloading = false;
				this.canShoot = true;
				p.sendMessage("§cReloadDone");
			}
		}

		if (this.isInitializeing && this.initializeTime == this.tick && this.alreadinit == false) {
			this.isInitializeing = false;
			this.canShoot = true;
			this.alreadinit = true;
			return;
		}

		if (this.lastshoot && this.shootspeed <= this.tick && this.isInitializeing == false && this.isReloading == false) {
			this.lastshoot = false;
			this.canShoot = true;
		}

	}

	public void shoot() {
		this.tick = 0;
		this.canShoot = false;
		this.lastshoot = true;
		this.nowAmount--;
		this.sound.playSound(SoundType.SHOOT, p.getLocation());

	}

	public int getNowAmount() {
		return this.nowAmount;
	}

	public boolean canShoot() {
		return this.canShoot;
	}

	public void reload() {
		this.tick = 0;
		this.canShoot = false;
		this.isReloading = true;
		this.nowAmount = this.defaultamount;
		this.sound.playSound(SoundType.RELOAD, p.getLocation());
	}

	public boolean isReloading() {
		return this.isReloading;
	}

	public ReloadHandler setInitializeTime(int i) {
		this.initializeTime = i;
		return this;

	}

	public ReloadHandler setShootSpeed(int i) {
		this.shootspeed = i;
		return this;
	}

	public ReloadHandler clone(SoundHandler hd, Player p, int amount) {
		return new ReloadHandler(this.initializeTime, this.shootspeed, this.reloadTime, this.defaultamount, hd, p, amount);
	}

	public boolean isIniting() {
		return this.isInitializeing;
	}

}
